package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.ResultCode;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.ServiceBusiness;
import com.wangsd.web.coupon.service.CompanyService;
import com.wangsd.web.coupon.service.ServiceBusinessService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-18
 */
@Api(value = "serviceBusiness", tags = "服务商管理")
@RestController
@RequestMapping("/coupon/serviceBusiness")
@Slf4j
public class ServiceBusinessController extends MyController {
    @Autowired
    ServiceBusinessService serviceBusinessService;
    @Autowired
    CompanyService companyService;
    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "查询服务商列表", notes = "查询服务商列表", httpMethod = "GET")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "serviceBusiness", value = "传入json格式") ServiceBusiness serviceBusiness) {
        SysUser userInfo = getUserInfo();
        IPage<ServiceBusiness> sysUserIPage = serviceBusinessService.selectPage(userInfo.getCode(), pageNum, pageSize, serviceBusiness);
        return Result.success(sysUserIPage);

    }

    @ApiOperation(value = "删除服务商", notes = "删除服务商", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "服务商id", required = true) @PathVariable Long id) {
        List<Company> companyList = companyService.list(new QueryWrapper<Company>().eq("business_service_id", id));
        if(companyList!=null&&companyList.size()>0){
            return Result.fail("请先删除服务商下的商圈！");
        }
        List<SysUser> sysUsers = sysUserService.list(new QueryWrapper<SysUser>().eq("parent_id", id).eq("type", 1));
        if(sysUsers!=null&&sysUsers.size()>0){
            return Result.fail("请先删除服务商下的服务商用户！");
        }
        boolean b = serviceBusinessService.removeById(id);
        return Result.success(b);

    }

    @ApiOperation(value = "新增或修改服务商信息", notes = "新增或修改服务商信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody ServiceBusiness serviceBusiness) {

        if (StringUtils.isBlank(serviceBusiness.getName())) {
            return new Result(PRARM_NOT_NULL.getCode(),"名称"+PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(serviceBusiness.getAppId())) {
            return new Result(PRARM_NOT_NULL.getCode(),"appId"+PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(serviceBusiness.getMchId())) {
            return new Result(PRARM_NOT_NULL.getCode(),"商户id"+PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(serviceBusiness.getMchKey())) {
            return new Result(PRARM_NOT_NULL.getCode(),"商户密钥"+PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(serviceBusiness.getKeyPath())) {
            return new Result(PRARM_NOT_NULL.getCode(),"密钥地址"+PRARM_NOT_NULL.getMessage());
        }

        serviceBusinessService.addOrUpdate(serviceBusiness);
        return Result.success();

    }

    @ApiOperation(value = "查询所有服务商", notes = "查询所有服务商", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<ServiceBusiness> list = serviceBusinessService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }
}

